<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Class Article_model
 * 文章操作模型
 * @author wulliang
 */
class Article_model extends CI_Model {
    private $tableName = null;
    private $preTableName = null;
    /**
     * 构造函数
     * 加载数据库类
     */
    public function __construct () {
        parent::__construct();
        $this->tableName = 'article';
        $this->preTableName = 'wl_article';
        $this->load->database();
    }

    /**
     * 获取首页推荐文章
     * @param int $cate 栏目ID
     * @return mixed
     */
    public function getRecommendArticle ($cate = null) {
        if ($cate) {
            $res = $this->db->select('aid,title,thumb,info,time,article.cid,cname,click,author,recommend')->from('article')->join('category', 'category.cid = article.cid')->order_by('time','asc')->where(array('recommend'=>1, 'article.cid'=>$cate,'valid'=>0))->limit(5)->get()->result_array();
        } else {
            $res = $this->db->select('aid,title,thumb,info,time,article.cid,cname,click,author,recommend')->from('article')->join('category','category.cid = article.cid')->order_by('time','asc')->where(array('recommend'=>1,'valid'=>0))->get()->result_array();
        }

        return $res;
    }

    /**
     * 获取文章列表
     * @param int $del 是否为删除文章
     * @return mixed
     */
    public function getAll ($del = 0) {
        $res = $this->db->select('aid,title,cname,click,time,recommend')->from('article')->join('category', 'category.cid = article.cid')->order_by('time','desc')->where(array('article.valid'=>$del))->get()->result_array();

        return $res;
    }

    /**
     * 根据id获取文章信息
     * @param int $aid 获取文章信息id
     * @return mixed
     */
    public function getOne ($aid) {
        $res = $this->db->select('aid,title,thumb,cname,type,article.cid,info,content')->from('article')->join('category', 'category.cid = article.cid')->order_by('time','desc')->where(array('article.valid'=>0,'article.aid'=>$aid))->get()->result_array();

        return $res[0];
    }

    /**
     * 查询记录总数
     * @param int $del 是否是删除文章
     * @return mixed
     */
    public function count ($del = 0) {
        $sql = "select count(*) as record from wl_article where valid = ".$del;
        $res = $this->db->query($sql)->result_array();
        return $res[0]['record'];
    }

    /**
     * 添加文章
     * @param array $data
     * @return mixed
     */
    public function add ($data) {
      return $this->db->insert($this->tableName,$data);
    }

    /**
     * 更新文章
     * @param int $aid 文章id
     * @param array $data
     * @return mixed
     */
    public function update ($aid, $data) {
        return $this->db->update($this->tableName, $data, array('aid'=>$aid));
    }

    /**
     * 删除 / 恢复 文章
     * @param int $aid 文章id
     * * @param int $valid  1: 物理删除文章  0 : 恢复删除文章
     * @return mixed
     */
    public function deleteOrRecoverOne ($aid,$valid) {
        return $this->db->update($this->tableName,array('valid'=>$valid),array('aid'=>$aid));
    }

    /**
     * 彻底删除文章
     * @param int $aid 文章Id
     * @return mixed
     */
    public function deleteOne ($aid) {
        return $this->db->delete($this->tableName)->where(array('aid'=>$aid));
    }

    /**
     * 文章推荐操作
     * @param $aid 文章ID
     * @param bool $flag FALSE => 推荐  TRUE => 取消推荐
     * @return mixed
     */
    public function recommend ($aid, $flag = FALSE) {
        if ( ! $flag) { // 站长推荐
            return $this->db->update($this->tableName,array('recommend'=>1),array('aid'=>$aid));
        } else {        // 取消推荐
            return $this->db->update($this->tableName,array('recommend'=>0),array('aid'=>$aid));
        }
    }

    /**
     * 文章批量操作
     * @param $aid 批量操作的文章id
     * @param $operation 操作类型 recommend => 批量推荐  delete => 批量删除
     * @return mixed 返回受影响的行数
     */
    public function multiple ($aid,$operation) {
        $where = 'aid in ('.$aid.')';
        switch ($operation) {
            case 'recommend' : // 批量推荐
                return $this->db->update($this->tableName, array('recommend'=>1), $where);
            break;
            case 'invalid' : // 批量删除
                return $this->db->update($this->tableName, array('valid'=>1), $where);
            break;
            case 'recover' : //批量恢复删除文章
                return $this->db->update($this->tableName, array('valid'=>0), $where);
            break;
            case 'delete' :
                return $this->db->delete($this->tableName)->where($where);
            break;
        }
    }

    /**
     * 计算推荐文章的条数
     * @param null $cate
     * @return mixed
     */
    public function countRecommend ($cate = null) {
        $sql = 'select count(*) as recommendNum from wl_article where recommend = 1';
        if ($cate) $sql .= ' and cid = '.$cate;
        $res = $this->db->query($sql)->result_array();
        return $res[0]['recommendNum'];
    }

    /**
     * 获取首页右侧tab切换数据
     * @param $type click 点击量  hot 热门文章 new 最新文章
     * @return mixed
     */
    public function getTableArticle ($type) {
        switch ($type) {
            case 'click' : $sql = "select aid,title from $this->preTableName order by click desc limit 6"; break;
            case 'new'   : $sql = "select aid,title from $this->preTableName order by time desc limit 6"; break;
            case 'hot'   : $sql = "select aid,title from $this->preTableName where recommend = 1 order by time desc limit 6"; break;
        }
        $query = $this->db->query($sql);
        $result = $query->result_array();

        return $result;
    }

    /**
     * 获取右侧图文推荐
     * @return mixed
     */
    public function getGraphicRecommend () {
        $count = $this->count();
        $start = mt_rand(0,$count-5);
        $end = $start + 5;
        $sql = "select a.aid,a.title,a.thumb,c.cname,a.time from wl_article a left join wl_category c on a.cid = c.cid order by time desc limit $start,$end";

        $query = $this->db->query($sql);
        $result = $query->result_array();
        return $result;
    }

    /**
     * 获取列表页的文章
     * @param $cid
     * @return mixed
     */
    public function getArticleList ($cid) {
        $field = 'a.aid,a.title,a.thumb,a.time,a.info,a.author,c.cname';
        $res = $this->db->select($field)->from('article a')
            ->join('category c', 'a.cid = c.cid')->where(array('a.cid'=>$cid,'valid'=>0))->get()->result_array();
        return $res;
    }

    /**
     * 计算某类文章的数目
     * @param $cid
     * @return mixed
     */
    public function categoryArticleCount ($cid) {
        $sql = 'select count(*) as articleNum from wl_article where cid = '.$cid;
        $res = $this->db->query($sql)->result_array();

        return $res[0]['articleNum'];
    }
}